package com.ffcs.cn.action.desktopcontrol.smComboBoxDatasetType;

import com.supermap.data.DatasetType;
import com.supermap.desktop.controls.ui.controls.SmDialog;
import com.supermap.desktop.controls.ui.controls.comboBox.SmComboBoxDatasetType;
import com.supermap.desktop.core.ui.controls.GridBagConstraintsHelper;
import com.supermap.desktop.core.utilties.DatasetTypeUtilities;
import com.supermap.desktop.core.utilties.DefaultValues;

import javax.swing.*;
import javax.swing.border.TitledBorder;
import java.awt.*;
import java.util.Arrays;
import java.util.List;


public class DialogSmComboBoxDatasetType extends SmDialog {
	private SmComboBoxDatasetType comboBoxDatasetType;

	private JCheckBox checkBoxHasAll;

	private JCheckBox checkBoxHasSimpleDataset;

	private JLabel labelSupportDatasetTypes;
	private JList<String> listSupportDatasetTypes;


	public DialogSmComboBoxDatasetType() {
		setTitle("SmComboBoxDatasetType 示例");
		setSize(490,350);
		initComponents();
		initLayout();
		initListener();
	}

	private void initListener() {
		//设置显示 “所有数据集”选项
		checkBoxHasAll.addActionListener(e -> comboBoxDatasetType.setHasAll(checkBoxHasAll.isSelected()));
		//设置显示 “简答数据集”选项
		checkBoxHasSimpleDataset.addActionListener(e -> comboBoxDatasetType.setHasSimpleDataset(checkBoxHasSimpleDataset.isSelected()));
		//设置显示数据集类型
		listSupportDatasetTypes.addListSelectionListener(e -> {
			List<String> list = listSupportDatasetTypes.getSelectedValuesList();
			if (list == null || list.isEmpty()){
				return;
			}
			DatasetType[] datasetTypes = list.stream().map(DatasetTypeUtilities::fromString).toArray(DatasetType[]::new);
			comboBoxDatasetType.setSupportedDatasetTypes(datasetTypes);
		});
	}

	private void initLayout() {
		this.setLayout(new GridBagLayout());
		this.add(getLeftPanel(), new GridBagConstraintsHelper(0, 0, 1, 1)
				.setInsets(GridBagConstraintsHelper.FRAME_CONTROL_GAP, GridBagConstraintsHelper.FRAME_CONTROL_GAP,
						GridBagConstraintsHelper.FRAME_CONTROL_GAP, GridBagConstraintsHelper.CONTROLS_GAP)
				.setWeight(1, 1)
				.setFill(GridBagConstraints.BOTH));

		this.add(getRightPanel(), new GridBagConstraintsHelper(1, 0, 1, 1)
				.setInsets(GridBagConstraintsHelper.FRAME_CONTROL_GAP, 0,
						GridBagConstraintsHelper.FRAME_CONTROL_GAP, GridBagConstraintsHelper.FRAME_CONTROL_GAP)
				.setWeight(1, 1)
				.setFill(GridBagConstraints.BOTH));
	}

	private Component getRightPanel() {
		JPanel panel = new JPanel();
		panel.setLayout(new GridBagLayout());
		panel.setBorder(new TitledBorder("控制面板"));

		panel.add(checkBoxHasAll, new GridBagConstraintsHelper(0, 0, 1, 1)
				.setInsets(GridBagConstraintsHelper.FRAME_CONTROL_GAP, GridBagConstraintsHelper.FRAME_CONTROL_GAP,
						0, GridBagConstraintsHelper.FRAME_CONTROL_GAP)
				.setAnchor(GridBagConstraints.WEST));

		panel.add(checkBoxHasSimpleDataset, new GridBagConstraintsHelper(0, 1, 1, 1)
				.setInsets(GridBagConstraintsHelper.CONTROLS_GAP, GridBagConstraintsHelper.FRAME_CONTROL_GAP,
						0, GridBagConstraintsHelper.FRAME_CONTROL_GAP)
				.setAnchor(GridBagConstraints.WEST));

		panel.add(labelSupportDatasetTypes, new GridBagConstraintsHelper(0, 2, 1, 1)
				.setInsets(GridBagConstraintsHelper.CONTROLS_GAP, GridBagConstraintsHelper.FRAME_CONTROL_GAP,
						0, GridBagConstraintsHelper.FRAME_CONTROL_GAP)
				.setAnchor(GridBagConstraints.WEST));

		JScrollPane scrollPane = new JScrollPane(listSupportDatasetTypes);
		scrollPane.setPreferredSize(new Dimension(100,100));
		panel.add(scrollPane, new GridBagConstraintsHelper(0, 3, 1, 1)
				.setInsets(GridBagConstraintsHelper.CONTROLS_GAP, GridBagConstraintsHelper.FRAME_CONTROL_GAP,
						GridBagConstraintsHelper.FRAME_CONTROL_GAP, GridBagConstraintsHelper.FRAME_CONTROL_GAP)
				.setAnchor(GridBagConstraints.WEST).setWeight(1,1).setFill(GridBagConstraints.BOTH));
		return panel;
	}

	private Component getLeftPanel() {
		JPanel panel = new JPanel();
		panel.setLayout(new GridBagLayout());
		panel.setBorder(new TitledBorder("控件"));
		comboBoxDatasetType.setPreferredSize(new Dimension(100,DefaultValues.getDefaultComponentHeight()));
		panel.add(new JLabel("数据集类型:"), new GridBagConstraintsHelper(0, 0, 1, 1)
				.setInsets(GridBagConstraintsHelper.FRAME_CONTROL_GAP, GridBagConstraintsHelper.FRAME_CONTROL_GAP, 0, 0)
				.setAnchor(GridBagConstraints.WEST));
		panel.add(comboBoxDatasetType, new GridBagConstraintsHelper(1, 0, 1, 1)
				.setInsets(GridBagConstraintsHelper.FRAME_CONTROL_GAP, GridBagConstraintsHelper.FRAME_CONTROL_GAP,
						0, GridBagConstraintsHelper.FRAME_CONTROL_GAP)
				.setAnchor(GridBagConstraints.WEST).setWeight(1, 1).setFill(GridBagConstraints.HORIZONTAL));
		return panel;
	}

	private void initComponents() {
		comboBoxDatasetType = new SmComboBoxDatasetType();

		checkBoxHasAll = new JCheckBox("显示 “所有数据集”选项");

		checkBoxHasSimpleDataset = new JCheckBox("显示 “简单数据集”选项");

		labelSupportDatasetTypes = new JLabel("显示的数据集类型：");
		listSupportDatasetTypes = new JList<>();
		DatasetType[] allDatasetType = DatasetTypeUtilities.getAllDesktopDatasetType();
		String[] types = Arrays.stream(allDatasetType).map(DatasetTypeUtilities::toString).toArray(String[]::new);
		listSupportDatasetTypes.setListData(types);
	}
}
